package afcp.alumni.action.base;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.integration.spring.SpringBean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import afcp.alumni.model.db.Member;
import afcp.alumni.util.SwallowingHttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;

public class MyActionBeanContext extends ActionBeanContext {

//	@Override
//	public Resolution getSourcePageResolution() {
////        String sourcePage = getSourcePage();
//        String uri = this.getRequest().getServletPath();
////        if (sourcePage == null) {
//        	
//			// bug dans une boucle infinie.
//            return new ForwardResolution(uri);
////        }
////        else {
////            return new ForwardResolution(sourcePage);
////        }
//	}

	@SpringBean
	private BlobstoreService blobstoreService;

	private static final Log logger = LogFactory.getLog(MyActionBeanContext.class);
	
	public static final String SESSION_USER = "user";
	public static final String SESSION_QUERY = "q";

	public void setUser(Member user) {
		getRequest().getSession().setAttribute(SESSION_USER, user);
	}

	public Member getUser() {
		return (Member) getRequest().getSession().getAttribute(SESSION_USER);
	}

	public void removeUser() {
		getRequest().getSession().removeAttribute(SESSION_USER);
	}
	
	public void setQuery(String query) {
		getRequest().getSession().setAttribute(SESSION_QUERY, query);
	}
	
	public String getQuery() {
		return (String) getRequest().getSession().getAttribute(SESSION_QUERY);
	}
	
	public Map<String, List<BlobKey>> getBlobs() {
		return blobstoreService.getUploads(this.getRequest());
	}


	/**
	 * <p>
	 * Execute the treatments to render the result of a request (like rendering
	 * a JSP file), but return it as a String instead of sending it back to the
	 * client.
	 * </p>
	 * <p>
	 * Example of use:
	 * </p>
	 * <p>
	 * {@code System.out.println(actionBean.forwardToString("/WEB-INF/jsp/home.jsp"));}
	 * </p>
	 * 
	 * @param url
	 *            to render
	 * @return the result rendered as a String
	 */
	public String forwardToString(String url) {
		
		StringWriter sout = new StringWriter();
		StringBuffer buffer = sout.getBuffer();

		HttpServletResponse realResponse = this.getResponse();
		HttpServletResponse fakeResponse = new SwallowingHttpServletResponse(
				realResponse, sout, realResponse.getCharacterEncoding());

		HttpServletRequest realRequest = this.getRequest();
		// realRequest.setAttribute(ATTRIBUTE_DWR, Boolean.TRUE);

		try {
			this.getServletContext().getRequestDispatcher(url)
					.forward(realRequest, fakeResponse);
		} catch (ServletException | IOException e) {
			logger.error("An error occurred while transforming a resolution into String", e);
		}

		return buffer.toString();
	}

//	private static final String SCOPE_KEY_FROM = "from";
//
//	public void registerSourcePageURL(String url) {
//		FlashScope scope = FlashScope.getCurrent(getRequest(), true);
//		scope.put(SCOPE_KEY_FROM, url);
//	}
//
//	public String getSourcePageActionBeanURL() {
//		FlashScope scope = FlashScope.getCurrent(getRequest(), true);
//		String url = (String) scope.get(SCOPE_KEY_FROM);
//		logger.info("getting URL: " + url);
//		return url;
//	}
	
//	public void setCurrentPage(String uri) {
//		this.getRequest().setAttribute("currentPage", uri);
//	}

}
